package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.mapper.WaterTypeMapper;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 根据客户查询用户列表
 * 
 * @author 甄玉晴 2020年4月7日
 */
public class QueryUserByCtm implements ServerInterface {

	@SuppressWarnings("rawtypes")
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			// 入参转换
			UserBean bean = JsonUtils.objectToPojo(req.getData(), UserBean.class);
			if (StringUtils.isBlank(bean.getCtmNo())) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, "客户编号不能为空。", Void.class);
			}
			UserInfoMapper mapper = factory.getMapper(UserInfoMapper.class);
			List<UserBean> list = new ArrayList<UserBean>();
			// 根据客户编号查询账户列表
			list = mapper.userInfoListByCtm(bean);
			//获取数据字典信息
			for(UserBean user:list) {
				// 获取数据字典信息
				if (StringUtils.isNotBlank(user.getStatus())) {
					user.setStatus(Tools.getDicNameByCodeAndValue(factory, "CUS", user.getStatus()));
				}
				//获取用水类型
				if(!StringUtils.isBlank(user.getUseWaterType())) {
					WaterTypeMapper waterTypeMapper=factory.getMapper(WaterTypeMapper.class);
					user.setUseWaterTypeName(waterTypeMapper.findWaterTypeById(user.getUseWaterType()).getWaterTypeName());
				}
				// 获取营业区域
				if (!StringUtils.isEmpty(user.getBusinessArea())) {
					BusinessAreaMapper businessAreaMapper = factory.getMapper(BusinessAreaMapper.class);
					user.setBusinessAreaName(
							businessAreaMapper.findBusinessAreaById(user.getBusinessArea(), userInfo.getCurWaterCode()));
				}
				if (StringUtils.isNotBlank(user.getUseWaterObject())) {
					user.setUseWaterObject(Tools.getDicNameByCodeAndValue(factory, "UWS", user.getUseWaterObject()));
				}
			}
			// 返回结果集
			MessageBean<List> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "sucess", List.class);
			msg.setData(list);
			return msg;
		} catch (Exception e) {
			logger.error("根据客户编号查询账户查询失败" + e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", Void.class);
		}
	}

}
